package com.avecsys.quotemanager.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SystemConfig;

/**
 * 
 * @author sam sundar K
 * 
 */
public class ValidatorEmail extends BaseValidator {

	private static final String EMAIL_PATTERN = "emailPattern";

	public FacesMessage validateFieldValue(FacesContext facesContext,
			UIComponent uIComponent, Object object) throws ValidatorException {
		String emailInputField = (String) object;
		FacesMessage message = new FacesMessage();
		Pattern emailPattern = Pattern.compile(SystemConfig
				.getProperty(EMAIL_PATTERN));
		Matcher emailMatcher = emailPattern.matcher(emailInputField);
		if (!emailMatcher.matches()) {
			message.setSummary(MessageBoardUtil.getValue(facesContext,
					ApplicationConstants.RESOURCE_BUNDLE_COMMON,
					"VALIDATION_INVALID_EMAIL_ADDRESS", null));
		}
		return message;
	}
}
